package com.bgi.uims.common.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * ClassName: BaseAction <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2014-10-20 下午02:07:23 <br/>
 *
 * @author fukun
 * @version 
 * @since JDK 1.7
 */
@SuppressWarnings("serial")
public class BaseAction extends ActionSupport implements ServletRequestAware,ServletResponseAware,SessionAware{

	public HttpServletRequest request;
	public HttpServletResponse response;
	public Map<String, Object> session;
	/**
	 * 当前页码
	 */
	private Integer[] page;
	/**
	 * 每页行数
	 */
	private Integer[] rows;
		
	public Integer[] getPage() {
		return page;
	}
	
	public void setPage(Integer[] page) {
		this.page = page;
	}
	
	public Integer[] getRows() {
		return rows;
	}
	
	public void setRows(Integer[] rows) {
		this.rows = rows;
	}
	
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request=request;
	}

	
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response=response;
	}

	
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session=session;
	}
	
	/**
	 * setParamFromJSON: 处理contentType application/json方式提交的json
	 * @param param JSON将转换成此类再赋值
	 * @throws IOException
	 * @return bean 返回参数类的实例
	 */
	public Object getParamFromJSON(Class<?> clazz) throws IOException{
		String JSONstr = null;
		BufferedReader br = request.getReader();
		String line = "";
		StringBuffer buff = new StringBuffer();
		while((line=br.readLine())!=null){
			buff.append(line);
		}
		
		JSONstr = buff.toString();
		Object bean = JSONObject.toBean(JSONObject.fromObject(JSONstr),clazz);
		
		return bean;
	}
	//物理路径
	public String getAppImgSaveRealPath(){
		return request.getSession().getServletContext().getRealPath("/") + "../uploadFiles/app/";
	}
	//web路径
	public String getAppImgSaveContextPath(){
		return request.getContextPath() + "/../uploadFiles/app/";
	}
}
